/**
 * 弹出注册框,注册界面
 */
function onRegister() {
    layer.open({
        type: 2,
        title: '注册',
        content: ['/login/register', 'no'],
        area: ['420px', '480px'],
        isOutAnim:true,
        anim:1,
        shade: [0.6, '#263849'],
        skin:'regskin',
        zIndex:10
    });
}

layui.use(['layer', 'form'], function () {
    var layer = layui.layer;
    var form = layui.form;
    form.verify({
        usernameR: function (value) {
            var reg = /^[1][3,4,5,7,8][0-9]{9}$/;//判断手机号的正则表达式
            if (value.length == 0 || value.trim() == '' || value == null) {
                return "请输入手机号";
            }
            if (!reg.test(value)) {
                return "请输入有效的手机号";
            }
        },
        passwordR: function (value) {
            var reg = /^[A-Z|a-z|0-9].{5,8}$/;//判断密码为6-8位
            if (value.length == 0 || value.trim() == '' || value == null) {
                return "请输入密码";
            } else if (!reg.test(value)) {
                return "密码格式不正确";
            }
        },
        passwordRC: function (value) {
            var passwdC = $("#passwd").val();
            if (value.length == 0 || value.trim() == '' || value == null) {
                return "请确认密码"
            }
            if (passwdC != value) {
                return "两次密码输入不一致";
            }
        },
        oldPasswd:function (value) {
            var reg = /^[A-Z|a-z|0-9].{5,8}$/;//判断密码为6-8位
            if (value.length == 0 || value.trim() == '' || value == null) {
                return "请输入旧密码";
            } else if (!reg.test(value)) {
                return "旧密码格式不正确";
            }
        },
        newPasswd:function (value) {
            var reg = /^[A-Z|a-z|0-9].{5,8}$/;//判断密码为6-8位
            if (value.length == 0 || value.trim() == '' || value == null) {
                return "请输入新密码";
            } else if (!reg.test(value)) {
                return "新密码格式不正确";
            }
        }
    });
    form.on('submit(register)',function (data) {
        // console.log(data.field);//页面数据
        // var username = data.field.username,
        //     password = data.field.passwordR,
        //     userIdentity='normal';
        var d = {
            'username': data.field.username,
            'password': data.field.passwordR,
            'userIdentity': 'normal'
        };
        console.log(d);//自定义上传数据
        $.ajax({
            type: 'post',
            url: '/login/toRegister',
            contentType: 'application/json',
            data: JSON.stringify(d),
            success: function (data) {
                if (data == 0) {
                    layer.msg('录入失败,用户已存在！', {
                        icon: 5,
                        offset: "auto",
                        time: 2000
                    });//提示框
                } else {
                    layer.msg('录入成功！', {
                        icon: 6,
                        offset: "auto",
                        time: 2000
                    });//提示框
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name);//获取窗口索引
                        parent.layer.close(index);//关闭弹出层
                    }, 1000);
                }
            },
            error:function(error) {
               layer.msg("error");
            }
        });
        return false;
    });
    form.on('submit(m_submit)',function (data) {
       $.ajax({
           type:'post',
           url:'/login/checkOldPassword',
           data:data.field,
           success:function (result) {
                if (result=="success"){
                    layer.msg("修改成功！",{
                        icon:1,
                        offset:['60px','470px'],
                        time:1000
                    },function () {
                        location.reload();
                    });
                }else if (result=="fail"){
                    layer.msg("旧密码错误，请重新输入！",{
                        icon:2,
                        offset:['60px','470px']
                    });
                }
           },
           error:function (error) {
                layer.msg("404");
           }
       });
       return false;
    });
});